package com.zigong.app.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.zigong.app.R;
import com.zigong.app.beans.NewsItem;
import com.zigong.app.http.AbsListNetCallback;
import com.zigong.app.http.NetUtils;
import com.zigong.app.ui.adapter.CommonFrgAdapter;
import com.zigong.app.ui.adapter.SpecialFrgAdapter;
import com.zigong.app.ui.util.RBaseItemDecoration;
import com.zigong.app.ui.utils.SkipUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by raoweiwei on 2017/8/3.
 * 首页 专题
 */

public class SpecialFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {
    @BindView(R.id.common_frg_refreshlayout)
    protected CanRefreshLayout mRefreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView mRecyclerView;
    private SpecialFrgAdapter mAdapter;
    private int mPageNo = 0;
    private boolean mIsLoading = false;
    private int mPageAll = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common_frg, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new RBaseItemDecoration(0));
    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (isFirst) {
            mRefreshLayout.autoRefresh();
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    public void onLoadMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mPageNo = 0;
        mIsLoading = true;
        getData();
    }


    private void getData() {
        NetUtils.getNetAdapter().getNewsPage(getOwnerName(), channelUrl, mPageNo, new AbsListNetCallback<NewsItem>(null) {
            @Override
            public void onSuc(List<NewsItem> newsItems) {
                if (!ListUtils.isListValued(newsItems)) {
                    onError(null, null);
                    return;
                }

                if (mPageAll == 0) {
                    mPageAll = getPageResponse().getData().getPageAll();
                }
                initData(newsItems, getPageResponse().getData().getTemplate());
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
            }

            @Override
            public void onEnd() {
                onNetFinish();
            }
        });
    }

    private void initData(List<NewsItem> newsItems, int template) {
        if (mAdapter == null) {
            mAdapter = new SpecialFrgAdapter(getActivity(), newsItems, template).setItemInternalClick(itemClick);
            mRecyclerView.setAdapter(mAdapter);
        }
        mAdapter.setAct(isAct);
        if (mPageNo == 0) {
            mAdapter.setData(newsItems);
        } else {
            mAdapter.addDatas(newsItems);
        }
        mPageNo++;
    }

    private synchronized void onNetFinish() {
        mIsLoading = false;
        mRefreshLayout.refreshComplete();
        mRefreshLayout.loadMoreComplete();
        mRefreshLayout.setLoadMoreEnabled(mPageNo >= mPageAll ? false : true);
    }

    OnItemInternalClick itemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
            pairs.add(new Pair<>("页面", "首页"));
            pairs.add(new Pair<>("栏目", tag));
            pairs.add(new Pair<>("行为类型", "点击"));
            GridsumWebDissector.getInstance().trackEvent(SpecialFragment.this, "", ((NewsItem) mAdapter.getItem(position)).getNewsTitle(), "", 200, pairs);
            HashMap<String, String> map = new HashMap<>();
            for (Pair<String, String> p : pairs) {
                map.put(p.first, p.second);
            }
            SkipUtil.skipWithNewsItem(getActivity(), (NewsItem) mAdapter.getItem(position), map);
        }
    };
}
